﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Social_VideoPostUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		ParsePostUploadData();
    }


	#region Post Upload
	public string VideoID
	{
		get
		{
			var id = Request.QueryString["id"];
			if (!string.IsNullOrEmpty(id))
				return id;
			else
				return "-1";
		}

	}
	public int VideoStatus
	{
		get
		{
			int status = -1;
			int.TryParse(Request.QueryString["status"], out status);
			return status;
		}
	}
	public string VideoStatusCode
	{
		get
		{
			return Request.QueryString["code"];
		}
	}
	public long PostID
	{
		get
		{
			long temp = 0;
			long.TryParse(Request.QueryString[AppParams.Keys.qs_postID], out temp);
			return temp;
		}
	}

	private void ParsePostUploadData()
	{
		var post = GetPost();
		if (post == null)
			return;

		if (VideoStatus == 200 && VideoID != "-1")
		{
			post.YtVideoId = VideoID;
			post.Update();
			lblMsg.Text = "Video uploaded successfully";
			ScriptManager.RegisterStartupScript(this, typeof(Page), "redirect", "parent.Redirect();", true);
		}
		else
		{
			lblMsg.Text = "Video was not accepted";
			Logic.BusinessLogic.Posting.UpdateUnprocessedPost(post.ID, BO.UnProcessedPost.ProcessStatuses.Rejected, VideoStatus.ToString() + "," + VideoStatusCode);
		}

	}

	private BO.Post GetPost()
	{
		BO.Post post = null;
		if (PostID != 0)
			post = BLL.Posts.GetByID(PostID);
		if (post != null)
			return post;
		lblMsg.Text = "Invalid post";

		return null;
	}


	#endregion
}